﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;

namespace CMU.Manage
{
    public partial class UCGiaoVien : UserControl
    {
        CertificateEntities context = new CertificateEntities();
        public UCGiaoVien()
        {
            InitializeComponent();
        }

        // LOAD 
        private void UCGiaoVien_Load(object sender, EventArgs e)
        {
            var loadgv = from gv in context.GiangVien    
                         select gv;
            dataGridView1.DataSource = loadgv.ToList();
            dataGridView1.Refresh();
        }

        // ADD
        private void button2_Click(object sender, EventArgs e)
        {

            GiangVien gv = new GiangVien();
            gv.TenGV = textBox2.Text;

            context.GiangVien.Add(gv);
            int count = context.SaveChanges();
            if (count > 0)
            {
                MessageBox.Show("Da them moi");
                UCGiaoVien_Load(sender, e);
            }
            else
                MessageBox.Show("Da ton tai");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int gvID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out gvID))
            {
                using (CertificateEntities newcontext = new CertificateEntities())
                {
                    GiangVien gv = new GiangVien() { Id = gvID };
                    newcontext.GiangVien.Attach(gv);
                    //context.ObjectStateManger.ChangeObjectState(mh, EntityState.Deleted);
                    //newcontext.Entry(mh).State = System.Data.EntityState.Deleted;
                    newcontext.GiangVien.Remove(gv);

                    int count = newcontext.SaveChanges();
                    if (count > 0)
                    {
                        MessageBox.Show("Da xoa");
                        UCGiaoVien_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Mon Hoc da duoc lien ket");
                    }

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int gvID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out gvID))
            {
                using (CertificateEntities context = new CertificateEntities())
                {

                    GiangVien giangvien = new GiangVien() { Id = gvID };
                    giangvien = (from gv in context.GiangVien
                              where gv.Id == gvID
                              select gv).FirstOrDefault<GiangVien>();

                    textBox2.Text = giangvien.TenGV;
                    

                }
            }
        }
    }
}
